﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XMK.Tadong.Biz;
using System.Data;
using XMK.Tadong.Model;
using HU8.Helpers.Files;
using System.Configuration;
using System.IO;

namespace XMK.Tadong.Web.WebFileExplorer.DataSubmit
{
    public partial class SubmitTeamData : System.Web.UI.Page
    {
        #region 变量

        //获取文档存储路径
        string dataPath = string.Empty;


        #endregion 变量

        #region 事件处理程序

        //提交点击
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
        }

        //提交按钮点击事件
        protected void btnSunmit_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.Request.QueryString["File_ID"]))
            {
                TB_XMK_FILEInfo model = new TB_XMK_FILEInfo();
                Tb_Xmk_FileBiz bll = new Tb_Xmk_FileBiz();
                model.FILE_ID = Convert.ToInt32(this.Request.QueryString["File_ID"]);
                model.TEAM_ID = Convert.ToInt32(hidIDS.Value);
                model.FILE_NAME = txtWDMC.Text.Trim();
                string file_name = model.FILE_NAME.ToString();
                string FILE_EXT = "."+file_name.Substring(file_name.IndexOf(".") + 1, file_name.Length);
                EXTBiz extbiz = new EXTBiz();
                ExtTypeinfo extmode = extbiz.getExtCodeByExtent(FILE_EXT);
                FILE_EXT = extmode.EXT_CODE;
                //if (FILE_EXT.Equals("doc") || FILE_EXT.Equals("docx") || FILE_EXT.Equals("DOC") || FILE_EXT.Equals("DOCX"))
                //{
                //    model.FILE_EXT = 2;

                //}
                //else if (FILE_EXT.Equals("xls") || FILE_EXT.Equals("xlsx") || FILE_EXT.Equals("XLS") || FILE_EXT.Equals("XLSX"))
                //{
                //    model.FILE_EXT = 3;

                //}
                //else if (FILE_EXT.Equals("pdf") || FILE_EXT.Equals("PDF"))
                //{
                //    model.FILE_EXT = 4;

                //}
                //else if (FILE_EXT.Equals("txt") || FILE_EXT.Equals("TXT"))
                //{
                //    model.FILE_EXT = 5;

                //}
                //else if (FILE_EXT.Equals("ppt") || FILE_EXT.Equals("pptx") || FILE_EXT.Equals("PPT") || FILE_EXT.Equals("PPTX"))
                //{
                //    model.FILE_EXT = 6;

                //}
                model.FILE_ABST = txtZYXX.Value.Trim();
                model.FILE_AUTHOR = txtZZ.Text.Trim();
                model.KEYW = txtGJC.Text.Trim();
                model.FILE_CRT_DATE = Convert.ToDateTime(paydate.Value.Trim());
                model.FILE_VERSION = 1;
                if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                model.SECRET_LVL = "";
                model.LANG_TYPE = 0;
                model.FILE_TYPE = "";
                model.SAVE_PATH = "Project/SourceFile";
                model.PDF_PATH = "Project/PDF";
                model.SWF_PATH = "Project/SWF";
               // model.USER_ID = Request.Cookies["_userId_"].Value;
                model.USER_ID = Session["user_id"].ToString();
                model.STATUS = 0;
                model.CREATE_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                if (bll.Add(model))
                {
                    //将文件复制到相应的课题组待审核的文件夹下
                    string newPath = ConfigurationManager.AppSettings["DiskPath"] + "Project/";
                    string OldPath = ConfigurationManager.AppSettings["DiskPath"] + hidPath.Value.Trim();
                    //获取文件名称
                    string FileName = Path.GetFileNameWithoutExtension(model.FILE_NAME);
                   
                    //原文件
                    string SourcePath = OldPath + "/SourceFile/" + model.FILE_NAME;
                    //pdf文件
                    string PDFPath = OldPath + "/PDF/" + FileName + ".pdf";
                    //swf文件
                    string SWFPath = OldPath + "/SWF/" + FileName + ".swf";




                    FileHelper.Copy(SourcePath, newPath + "SourceFile");
                    FileHelper.Copy(PDFPath, newPath + "PDF");
                    FileHelper.Copy(SWFPath, newPath + "SWF");
                    lblError.Text = "提交成功";
                }
            }

        }

        #endregion 事件处理程序

        #region 函数集

        //页面加载函数 
        public void InitData()
        {

            string File_ID = string.Empty;
            if (!string.IsNullOrEmpty(this.Request.QueryString["path"]))
            {
                hidPath.Value = this.Request.QueryString["path"].ToString();
            }
            //获取文档名称
            if (!string.IsNullOrEmpty(this.Request.QueryString["filename"]))
            {
                txtWDMC.Text = this.Request.QueryString["filename"].ToString();
            }
            if (!string.IsNullOrEmpty(this.Request.QueryString["File_ID"]))
            {
                File_ID = this.Request.QueryString["File_ID"].ToString();
            }
            if (!string.IsNullOrEmpty(File_ID))
            {
                TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                DataTable dt = bll.GetModel(File_ID);
                if (dt.Rows.Count > 0)
                {
                    hidIDS.Value = dt.Rows[0]["TEAM_ID"].ToString();
                    txtWDMC.Text = dt.Rows[0]["FILE_NAME"].ToString();
                    txtZYXX.Value = dt.Rows[0]["FILE_ABST"].ToString();
                    txtZZ.Text = dt.Rows[0]["FILE_AUTHOR"].ToString();
                    txtGJC.Text = dt.Rows[0]["KEYW"].ToString();
                    paydate.Value = dt.Rows[0]["FILE_CRT_TIME"].ToString();
                    txtMB.Text = dt.Rows[0]["DEST_OBJ"].ToString();
                    txtYWDX.Text = dt.Rows[0]["BIZ_OBJ"].ToString();

                }

            }

        }


        #endregion 函数集
    }
}